package com.archerdu.apply.controller.admin;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.Article;
import com.archerdu.apply.model.ArticleCatalog;
import com.archerdu.apply.model.User;
import com.archerdu.apply.service.ArticleCatalogManager;
import com.archerdu.apply.service.ArticleManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.base.dao.IPage;
import com.archerdu.commons.constant.DataConstant;
import com.archerdu.commons.constant.ErrorConstant;

@Controller
@Scope("prototype")
@RequestMapping("/admin/article")
public class ArticleController extends BaseController {

    private static final long     serialVersionUID = 3298072203679257178L;

    private ArticleCatalogManager articleCateManager;
    private ArticleCatalog        articleCate;
    private ArticleManager        articleManager;
    private Article               article;

    @RequestMapping("/main")
    public String main() {
        return "admin/article/main";
    }

    @RequestMapping(value = "/add")
    public ModelAndView addI(HttpServletRequest requ, ArticleController mc) {
        ModelAndView mv = new ModelAndView();
        // 没有目录不能添加文章
        if (mc.getArticle() == null || null == mc.getArticle().getCatalogId()
                || null == articleCateManager.get(mc.getArticle().getCatalogId())) {
            this.addMessage(ErrorConstant.ARTICLE_CATE_NOT_NULL);
            mv.setViewName("common/error");
        } else {
            article = mc.getArticle();

            mv.setViewName("admin/article/add");

            if ("get".equalsIgnoreCase(requ.getMethod())) {
                mv.addObject(article);
            } else {
                try {
                    article.setCreateUser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER))
                            .getId());
                    article.setCreatetime(getSqlDate());
                    articleManager.add(article);
                    addMessage(DataConstant.COMMON_ADD_SUCCESS);
                } catch (Exception e) {
                    addMessage(DataConstant.COMMON_ADD_FAILED);
                    logger.error(e.getMessage(), e);
                }
            }
        }
        mv.addObject(getMessage());
        return mv;
    }

    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, ArticleController mc) {

        logger.debug("article.id is " + mc.getArticle().getId());
        article = mc.getArticle();

        if ("get".equalsIgnoreCase(requ.getMethod())) {
            if (!StringUtils.isEmpty(article.getId())) {
                article = articleManager.get(article.getId());
            }
        } else {
            article.setUpdateUser(((User) requ.getSession().getAttribute((DataConstant.SESSION_SYS_USER))).getId());
            article.setUpdatetime(getSqlDate());
            try {
                articleManager.modify(article);
                article = articleManager.get(article.getId());
                addMessage(DataConstant.COMMON_EDIT_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_EDIT_FAILED);
                logger.error(e.getMessage(), e);
            }
        }

        ModelAndView mav = new ModelAndView();
        mav.addObject(getMessage());
        mav.addObject(article).setViewName("admin/article/edit");
        return mav;
    }

    @RequestMapping(value = "/remove")
    public ModelAndView remove(HttpServletRequest requ, ArticleController mc, Integer pageNo, Integer pageSize) {
        article = mc.getArticle();
        if (null != article && null != article.getId()) {
            article = articleManager.get(article.getId());
            article.setUseFlag(DataConstant.COMMON_USE_FLAG_NO);
            try {
                articleManager.modify(article);
                addMessage(DataConstant.COMMON_DEL_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_DEL_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        ModelAndView mav = page(requ, mc, pageNo, pageSize);
        mav.addObject(getMessage());
        return mav;
    }

    @RequestMapping(value = "/page")
    public ModelAndView page(HttpServletRequest requ, ArticleController ac, Integer pageNo, Integer pageSize) {

        if (null == ac.getArticle()) {
            article = new Article();
        } else {
            article = ac.getArticle();
        }

        if (null == pageNo || null == pageSize) {
            pageNo = 1;
            pageSize = 20;
        }
        logger.debug("articleManager is " + articleManager);
        article.setUseFlag(DataConstant.COMMON_USE_FLAG_YES);
        IPage<Article> page = articleManager.pageSearch(article, pageNo, pageSize);

        ModelAndView mav = new ModelAndView();
        mav.addObject(article);
        mav.addObject(page).setViewName("admin/article/page");
        return mav;
    }

    public ArticleCatalogManager getArticleCateManager() {
        return articleCateManager;
    }

    public void setArticleCateManager(ArticleCatalogManager articleCateManager) {
        this.articleCateManager = articleCateManager;
    }

    public ArticleCatalog getArticleCate() {
        return articleCate;
    }

    public void setArticleCate(ArticleCatalog articleCate) {
        this.articleCate = articleCate;
    }

    public ArticleManager getArticleManager() {
        return articleManager;
    }

    public void setArticleManager(ArticleManager articleManager) {
        this.articleManager = articleManager;
    }

    public Article getArticle() {
        return article;
    }

    public void setArticle(Article article) {
        this.article = article;
    }
}
